
var map;
var marker;
var geocoder;
var infowindow;
var diaChi = "";
var tinhThanh="";
var quanHuyen="";
var duong="";
var soNha="";


$(document).ready(function(){
		initTinyMCE();
		initCreateMapNewLoc();
		$("#PlaceRevision_Street").keyup(function(){
			updateAddressLabel();
		});
		$('#PlaceRevision_AddressNumber').keyup(function(){
			updateAddressLabel();
			if($("option:selected", '#parentAddressPartContainer').val() !=''){
				tinhThanh = $("option:selected", '#parentAddressPartContainer').text();
			}
			if($("option:selected", '#childAddressPartContainer').val() !=''){
				quanHuyen = $("option:selected", '#childAddressPartContainer').text();
			}
			if($("#PlaceRevision_Street").val() !=''){
				duong = $("#PlaceRevision_Street").val();
			}
			if($("#PlaceRevision_AddressNumber").val()){
				soNha = $("#PlaceRevision_AddressNumber").val();
			}
			if(tinhThanh != "" && quanHuyen != "" && duong != "" && soNha != ""){
				diaChi = soNha + " " + duong + " - " + quanHuyen + " - " + tinhThanh;
				findLocation(diaChi, map, marker)
			}
			
		});
});

function findLocation(address, myMap, marker)
{
	if (!geocoder) {
		geocoder = new google.maps.Geocoder();
	}
	var geocoderRequest = { address: address };
	geocoder.geocode(geocoderRequest, function (results, status) {
		if (status == google.maps.GeocoderStatus.OK) 
		{
			myMap.setCenter(results[0].geometry.location);
			myMap.setZoom(15);
			if (marker == null) {
				marker = new google.maps.Marker({
					map: myMap,
					draggable: true
				});
			}
			if(infowindow == null)
			{
				infowindow = new google.maps.InfoWindow();
			}
			infowindow.setContent(results[0].formatted_address);
			infowindow.open(map, marker);
			google.maps.event.addListener(marker, 'click', toggleBounce);
			google.maps.event.addListener(marker, 'dragend', function () {
				document.getElementById('Lat').value = marker.getPosition().lat();
				document.getElementById('Lng').value = marker.getPosition().lng();
			});
			marker.setPosition(results[0].geometry.location);
			document.getElementById('Lat').value = marker.getPosition().lat();
			document.getElementById('Lng').value = marker.getPosition().lng(); 	
		}
	}); 
}
function UpdateDistrict(){
	updateAddressLabel();
	var value = $('#childAddressPartContainer select').val();
	if(value!=0){
		$("#PlaceRevision_Street").removeAttr("Disabled");
		$("#PlaceRevision_Street").val("");
	}else{
		$("#PlaceRevision_Street").attr('Disabled','Disabled');
		$("#PlaceRevision_Street").val("");
	}
}
function UpdateCity(){
	var idTinhThanh = $('.addressPart').val();
	var comboboxId = parseInt($(".comboboxId", $('.addressPart').parent()).first().val());
	$(".addressPartContainer").each(function() {
		if (parseInt($(".comboboxId", this).first().val()) > comboboxId)
		$(this).remove();
	}); 
	updateAddressLabel();
	$("#PlaceRevision_Street").attr('Disabled','Disabled');
	$("#PlaceRevision_Street").val("");
	$("#PlaceRevision_AddressNumber").val("");
	if(idTinhThanh>0){
		$.ajax({
			url:"controller/xl_tim_quan.php",
			type:"POST",
			dataType:"json",
			data: { idTT: idTinhThanh },
			success: function (data) {				
				if (data.length > 0)
				{
					var container = $("<div>").addClass("addressPartContainer").appendTo($("#childAddressPartContainer"));
					$("<input type='hidden' value='" + (comboboxId + 1) + "' />").addClass("comboboxId").appendTo(container);
					var childCbb = $("<select id='quanhuyen' onchange='UpdateDistrict()'>").addClass("addressPart").appendTo(container);
					childCbb.append("<option value=''>- Chọn Quận/Huyện -</option>");
					for($i=0;$i<data.length;$i++){
						childCbb.append("<option value='" + data[$i].MaQuan + "'>" + data[$i].TenQuan + "</option>");
					}
				}
			}
		})
	}
}

function updateAddressLabel() {
	var details = "";
	$(".addressPart").each(function() {
		if ($(this).val() && $(this).val() > 0) {
			if (details != "")
				details = $("option:selected", this).text() + " - " + details;
			else
				details = $("option:selected", this).text();
		}
	});
	if ($("#PlaceRevision_Street").val() != "") {
		if (details != "")
			details = $("#PlaceRevision_Street").val() + " - " + details;
		else
			details = $("#PlaceRevision_Street").val();
	} 
	if ($("#PlaceRevision_AddressNumber").val() != "") {
		if (details != "")
			details = $("#PlaceRevision_AddressNumber").val() + " - " + details;
		else
			details = $("#PlaceRevision_AddressNumber").val();
	}
	$("#addressConcat").html(details);
	$("#PlaceRevision_Address").val(details);
	$('#Quan').val($('#quanhuyen').val());
} 
function initCreateMapNewLoc(){
	var myOptions = {
	  zoom: 5,
	  center: new google.maps.LatLng(17.1558349081035, 106.652685165405),
	  mapTypeId: google.maps.MapTypeId.ROADMAP,
	  disableDefaultUI: true,
	  mapTypeControl: false,
	  mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU, 
	  mapTypeIds: [google.maps.MapTypeId.ROADMAP,google.maps.MapTypeId.SATELLITE] },
	  navigationControl: true, 
	  navigationControlOptions: { position: google.maps.ControlPosition.TOP, style: google.maps.NavigationControlStyle.ANDROID }, 
	  scaleControl: false, 
	  keyboardShortcuts: true, 
	  disableDoubleClickZoom: false, 
	  draggable: true, 
	  scrollwheel: true, 
	  streetViewControl: false, 
	  noClear: false, 
   };
   map = new google.maps.Map(document.getElementById("map"), myOptions);
   if (marker == null) {
		marker = new google.maps.Marker({
			map: map,
			draggable: true,
			animation: google.maps.Animation.BOUNCE
		});
	}
	google.maps.event.addListener(marker, 'click', toggleBounce); 
}
function toggleBounce() {
	if (marker.getAnimation() != null) {
		marker.setAnimation(null);
	} else {
		marker.setAnimation(google.maps.Animation.BOUNCE);
	}
} 
function initTinyMCE() {
    tinyMCE.init({ mode: "exact", theme: "advanced", skin: "o2k7", elements: "noidung", plugins: "autolink,tabfocus,paste", content_css: false, theme_advanced_buttons1: "bold,italic,underline,|,fontsizeselect,forecolor,|,link,unlink,|,removeformat", theme_advanced_buttons2: "", theme_advanced_toolbar_location: "top", theme_advanced_toolbar_align: "left", theme_advanced_resizing: false, tab_focus: ":prev,:next", relative_urls: false, height: "300", paste_text_sticky: true, paste_auto_cleanup_on_paste: true, paste_remove_styles: true, paste_remove_spans: true, setup: function (a) { a.onInit.add(function (b) { b.pasteAsPlainText = true }) } })
}